package com.demo.controller;

import com.demo.bean.FixedEvent;
import com.demo.bean.User;
import com.demo.service.FixedEventService;
import com.demo.service.RedisService;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author 李天航
 */
@RestController
@RequestMapping("/fixedevent")
public class FixedEventController {
    @Resource
    FixedEventService fixedEventService;
    @Resource
    RedisService redisService;
    /**
     * 添加一项固定活动
     * @param fixedEvent 添加的活动信息
     * @param session 缓存
     * @return status,msg,如果有冲突会有addFixedEvent和ConflictFixedEvent
     */
    @PostMapping("/add")
    public Object addFixedEvent(FixedEvent fixedEvent, HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        fixedEvent.setUserAccount(user.getUserAccount());
        return fixedEventService.addFixedEvent(fixedEvent);
    }

    /**
     * 删除一项固定活动
     * @param fixedEventId 固定活动的编号
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/delete")
    public Object deleteFixedEvent(Integer fixedEventId,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        return fixedEventService.deleteFixedEvent(fixedEventId,user.getUserAccount());
    }

    /**
     * 获得当前周的所有固定获得
     * @param weekNum 周数
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/getall")
    public Object getAllFixedEvents(Long weekNum, HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        return fixedEventService.getFixedEvents(new FixedEvent(user.getUserAccount(),weekNum));
    }

    /**
     * 更新一项固定获得
     * @param fixedEvent 固定活动的信息
     * @param session 缓存
     * @return status,msg,如果有冲突会有updateFixedEvent和ConflictFixedEvent
     */
    @PostMapping("/update")
    public Object updateFixedEvent(FixedEvent fixedEvent,HttpSession session)
    {
        User user = (User)  redisService.getSessionData(session.getId(),"user");
        fixedEvent.setUserAccount(user.getUserAccount());
        return fixedEventService.updateFixedEvent(fixedEvent);
    }
}
